//
//  SEGDigitalManager.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/6.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGDigitalManager.h"



@implementation SEGDigitalManager
/**
 * 住户好评
 * @author Sam
 * @param quizTypeId  类型，值为：10010
 * @param communityId  小区ID
 * @param pageNo  页码
 * @param pageSize  条目数
 */
-(void)requestFiveStarWithquizTypeId:(NSString*)quizTypeId
                         communityId:(NSString*)communityId
                              pageNo:(NSInteger)pageNo
                            pageSize:(NSInteger)pageSize
                            Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
                             @"quizTypeId" : ValueToString(quizTypeId),
                             @"organId" : ValueToString(communityId),
                             @"quizRangeId" : @"1",
                             @"pageNo" : @(pageNo),
                             @"pageSize" : @(pageSize)
                             };
    
    __block NSInteger page = pageNo;
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/quiz/findQuizListByQuizType.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:urlString
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             if (isSuccessed) {
                                                 NSDictionary* dicData = [result valueForKey:@"data"];
                                                 NSArray* quizList = [dicData valueForKey:@"quizList"];
                                                 NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:quizList.count];
                                                 
                                                 for (NSDictionary* dic in quizList) {
                                                     SEGNewQuizModel* _model = [SEGNewQuizModel yy_modelWithJSON:dic];
                                                     [arrM addObject:_model];
                                                 }
                                                 
                                                 if (page != 1 && arrM.count == 0) {
                                                     finished(NO, codeType, @"没有更多数据了！");
                                                     return;
                                                 }
                                                 
                                                 NSMutableArray *arrM1 = [NSMutableArray arrayWithArray:self.dataQuizSource];
                                                 if (page == 1) {
                                                     [arrM1 removeAllObjects];
                                                 }
                                                 
                                                 [arrM1 addObjectsFromArray:arrM];
                                                 self.dataQuizSource = [arrM1 copy];
                                             }
                                             finished(isSuccessed, codeType, errorMessage);
                                         }];
}

/**
 v4.9.0
优秀员工活动列表

@param pageIndex 页数
@param pageSize 分页大小
@param finishe 回调
*/
- (void)requestPropertyAppraisalListWithPageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize finished:(SEGRequestCallBack)finished {
    NSDictionary *param = @{@"pageNo":@(pageIndex), @"pageLimit":@(pageSize)};
    WeakSelf;
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/bms-api/empee/activityList",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:param finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            NSArray *dicArray = result[@"data"][@"activityList"];
            if (![dicArray isKindOfClass:[dicArray class]]) {
                finished(NO, codeType, errorMessage);
                return;
            }
            if (dicArray.count == 0 && pageIndex != 1) {
                finished(NO, codeType, kNoMoreDataMessage);
                return;
            }
            
            NSInteger totalCount = [result[@"data"][@"paginator"][@"totalCount"] integerValue];
            NSMutableArray<SEGPropertyAppraisalModel *> *modelArray = [NSMutableArray arrayWithCapacity:dicArray.count];
            for (NSDictionary *subDic in dicArray) {
                SEGPropertyAppraisalModel *model = [SEGPropertyAppraisalModel yy_modelWithDictionary:subDic];
                [modelArray addObject:model];
            }
            if (pageIndex == 1) {
                weakSelf.propertyAppraisalDatas = [modelArray copy];
            } else {
                // 防止重复请求时数据有误
                if (weakSelf.propertyAppraisalDatas.count < totalCount) {
                    NSArray *oldData = [NSArray arrayWithArray:weakSelf.propertyAppraisalDatas];
                    weakSelf.propertyAppraisalDatas = [oldData arrayByAddingObjectsFromArray:modelArray];
                }
            }
            weakSelf.totalPages = [result[@"data"][@"paginator"][@"totalPages"] integerValue];
        }
        finished(isSuccessed, codeType, errorMessage);
    }];
}

- (void)requestPropertyAppraisalFaverWithEmployeeId:(NSString *)EmployeeId finished:(SEGRequestCallBack)finished {
    NSDictionary *param = @{@"excellentEmpeeId":(EmployeeId ?: @"")};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/bms-api/empee/praise",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:param finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(isSuccessed, codeType, errorMessage);
    }];
}
    
-(NSArray<SEGNewQuizModel *> *)dataQuizSource{
    if (!_dataQuizSource) {
        _dataQuizSource = [NSArray array];
    }
    return _dataQuizSource;
}

@end

